/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.processor.active;

import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.enums.Symbol;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.manage.core.domain.Organization;
import info.peigen.hotpot.manage.core.service.order.OrganizationEditOrder;
import info.peigen.hotpot.manage.core.service.order.UserEditOrder;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(OrganizationActiveProcessor)</b>
 * "组织机构" 激活处理器
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-22
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Component
public class OrganizationActiveProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 21;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        switchOrder(serviceContext);
    }

    private void switchOrder(ProcessorServiceContext serviceContext) {
        ServiceOrderBase contextOrder = serviceContext.getOrder();
        if (contextOrder instanceof OrganizationEditOrder order) {
            Organization domain = organizationRepository.active(order.getOrganizationId());

            serviceContext.setDomain(domain);
        }
        else if (contextOrder instanceof UserEditOrder order && StrUtil.isNotEmpty(order.getOrganizationIds())) {
            List<Organization> domains = organizationRepository.active(StrUtil.split(order.getOrganizationIds(), Symbol.COMMA.code()));
            serviceContext.getExtend().put("domains", domains);
        }

    }

}